package com.km.noh.weatherinfo.async;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Element;

import android.app.Activity;
import android.os.AsyncTask;
import android.widget.ImageView;

import com.km.noh.weatherinfo.WeatherInfoActivity;
import com.km.noh.weatherinfo.data.WeatherInfo;
import com.km.noh.weatherinfo.util.KMDialog;
import com.km.noh.weatherinfo.util.KMPreference;
import com.km.noh.weatherinfo.util.KMServerNetwork;
import com.km.noh.weatherinfo.util.KMStringUtil;
import com.km.noh.weatherinfo.util.Util;

public class WeatherDataAsync extends AsyncTask<String, Integer, String> {
	public static final String googleDomain = "http://www.google.co.kr";
	public static final String weatherUrl = googleDomain+"/ig/api";
	private WeatherInfoActivity mActiviy;
	public WeatherDataAsync(WeatherInfoActivity mActiviy) {
		this.mActiviy = mActiviy;
	}

	@Override
	protected String doInBackground(String... params) {
		KMServerNetwork network = new KMServerNetwork();
		String errorMsg="Server Error!!!";
		try {
			//city name
			HashMap<String, String> param = new HashMap<String, String>();
			String city=KMPreference.getInstance(mActiviy).getData(KMPreference.SELECTED_CITY, "Seoul");
			String cityKo=KMPreference.getInstance(mActiviy).getData(KMPreference.SELECTED_CITY_KO, "서울");
			param.put("weather",city);
			
			network.getData(weatherUrl,null, param, false);
			if (network.parser("problem_cause")==null) {
				Element e = network.getParser();
				WeatherInfoActivity.weatherData.put(WeatherInfo.TAG_CITY,cityKo);
				WeatherInfoActivity.weatherData.put(WeatherInfo.TAG_FORECAST_DATE,Util.parseXmlDocument(e, WeatherInfo.TAG_FORECAST_DATE,0));
				WeatherInfoActivity.weatherData.put(WeatherInfo.TAG_CONDITION,Util.parseXmlDocument(e, WeatherInfo.TAG_CONDITION,0));
				WeatherInfoActivity.weatherData.put(WeatherInfo.TAG_TEMP_C,Util.parseXmlDocument(e, WeatherInfo.TAG_TEMP_C,0));
				WeatherInfoActivity.weatherData.put(WeatherInfo.TAG_TEMP_F,Util.parseXmlDocument(e, WeatherInfo.TAG_TEMP_F,0));
				WeatherInfoActivity.weatherData.put(WeatherInfo.TAG_HUMIDITY,Util.parseXmlDocument(e, WeatherInfo.TAG_HUMIDITY,0));
				WeatherInfoActivity.weatherData.put(WeatherInfo.TAG_ICON,Util.parseXmlDocument(e, WeatherInfo.TAG_ICON,0));
				WeatherInfoActivity.weatherData.put(WeatherInfo.TAG_WIND_CONDITION,Util.parseXmlDocument(e, WeatherInfo.TAG_WIND_CONDITION,0));
				int cnt = Util.parseXmlDocument(e, WeatherInfo.TAG_FORECAST_CONDITIONS).length;
				Util.log("cnt ===>"+cnt);
				ArrayList<WeatherInfo> forecastList = new ArrayList<WeatherInfo>();
				for (int i=0;i<cnt;i++) {
					forecastList.add(new WeatherInfo(e,WeatherInfo.TAG_FORECAST_CONDITIONS,i));
				}
				WeatherInfoActivity.weatherData.put(WeatherInfo.TAG_FORECAST_CONDITIONS, forecastList);
				errorMsg = null;
			}else
				errorMsg =network.parser("problem_cause");
		}catch (Exception e) {
			Util.logError(e);
			
		}
		return errorMsg;
	}
	@Override
	protected void onPreExecute() {
		KMDialog.showProgress(mActiviy);
		super.onPreExecute();
	}
	@Override
	protected void onPostExecute(String tempData) {
		KMDialog.closeProgress(mActiviy);
		if (tempData != null) 
			KMDialog.showAlert(mActiviy,tempData);
		else
			mActiviy.onSyncComplete(OnDataSyncCompleteListener.RESULT_SYNC_SUCCESS);
	}
}
